<template>
  <div class="shop">
    <div style="text-align: left">
      <el-button
        type="primary"
        size="small"
        icon="el-icon-circle-plus"
        @click="addShop"
        >新增店铺
      </el-button>
    </div>
    <el-table height="650" :data="tableData" style="width: 100%">
      <template slot="empty">
        <img
          class="empty-img"
          src="@/assets/imgs/zanwushuju.svg"
          style="width: 64px; height: 50px"
          alt=""
        />
        <div class="noCart">暂无数据</div>
      </template>
      <el-table-column
        type="selection"
        align="left"
        width="100"
      ></el-table-column>
      <el-table-column
        label="#"
        type="index"
        align="left"
        width="60"
      ></el-table-column>
      <el-table-column
        prop="shop_title"
        align="center"
        label="店铺名称"
        width=""
      >
      </el-table-column>
      <el-table-column prop="shop_type" align="left" label="平台" width="">
      </el-table-column>
      <el-table-column
        prop="membe.username"
        align="left"
        label="已授权"
        width=""
      >
      </el-table-column>
      <el-table-column
        prop="create_time"
        align="left"
        label="创建时间"
        width=""
      >
        <template slot-scope="scope">
          {{ scope.row.create_time }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="left" label="操作" width="">
        <template slot-scope="scope">
          <el-button @click="editClick(scope.row)" type="text" size="small"
            ><i class="el-icon-edit"></i> 编辑
          </el-button>
          <el-button @click="delShop(scope.row)" type="text" size="small"
            ><i class="el-icon-delete"></i> 删除
          </el-button>
          <el-button @click="empowerShop(scope.row)" type="text" size="small"
            ><i class="el-icon-user"></i> 授权
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination :child-msg="pageparm" @callFather="callFather"></Pagination>
    <EditModalShop
      @submit="submitClick"
      @closeForm="closeDialog"
      :dialogVisible.sync="dialogVisible"
      :editData="editData"
      :dialogTitle="dialogTitle"
    ></EditModalShop>
  </div>
</template>

<script>
import {
  getShopList,
  addShop,
  updateShop,
  delShop,
  shopEmpower,
  getUpdateInfo,
} from "@/api/shop";
import EditModalShop from "./components/EditModal.vue";

export default {
  components: {
    EditModalShop,
  },
  data() {
    return {
      formInline: {
        page: 1,
        limit: 20,
      },
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 20,
        total: 0,
      },
      tableData: [],
      dialogVisible: false,
      dialogTitle: "",
      editData: {},
    };
  },
  created() {
    this.getShopList(this.formInline);
  },
  methods: {
    //获取列表
    async getShopList(obj) {
      const res = await getShopList(obj);
      this.tableData = res.data.data.data;
      //给分页器赋值
      this.pageparm.currentPage = res.data.data.current_page;
      this.pageparm.total = res.data.data.total;
      this.pageparm.pageSize = res.data.data.per_page;
    },
    //新增店铺
    addShop() {
      this.editData = {};
      this.dialogTitle = "新增";
      this.dialogVisible = true;
    },
    //修改店铺
    async editClick(row) {
      const res = await getUpdateInfo({ shop_id: row.shop_id });
      if (res.data.status === 200) {
        this.editData = res.data.data;
        this.dialogTitle = "编辑";
        this.dialogVisible = true;
      } else {
        this.$message.error(res.data.msg);
      }
    },
    //授权店铺
    empowerShop(row) {
      this.editData = row;
      this.dialogTitle = "授权";
      this.dialogVisible = true;
    },
    //新增（编辑）(授权)提交
    submitClick(val) {
      if (val.company_id) {
        if (val.membe_id) {
          this.shopEmpower({ shop_id: val.shop_id, membe_id: val.membe_id });
        } else {
          this.updateSaveShop(val);
        }
      } else {
        this.addSaveShop(val);
      }
    },
    //新增保存
    async addSaveShop(val) {
      const res = await addShop(val);
      if (res.data.status === 200) {
        this.dialogVisible = false;
        this.getShopList(this.formInline);
        this.$message({
          type: "success",
          message: res.data.msg,
        });
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    //修改保存
    async updateSaveShop(data) {
      const res = await updateShop(data);
      if (res.data.status === 200) {
        this.dialogVisible = false;
        this.getShopList(this.formInline);
        this.$message({
          type: "success",
          message: res.data.msg,
        });
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    //删除店铺
    async delShop(row) {
      const res = await delShop({ shop_id: row.shop_id });
      if (res.data.status === 200) {
        this.getShopList(this.formInline);
        this.$message({
          type: "success",
          message: res.data.msg,
        });
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    //店铺授权
    async shopEmpower(data) {
      const res = await shopEmpower(data);
      if (res.data.status === 200) {
        this.dialogVisible = false;
        this.getShopList(this.formInline);
        this.$message({
          type: "success",
          message: res.data.msg,
        });
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    //关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
    },
    //分页插件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getShopList(this.formInline);
    },
  },
};
</script>
<style lang="scss" scoped>
.shop {
  padding: 20px 10px;
}
</style>
